﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zor_IDAL;
using Zor_Modes;
using Zor_Tools;

namespace Zor_DAL
{
    public class Zor_RepealJinSan_DAL<T> where T : class
    {
        /// <summary>
        /// 金三销号
        /// </summary>
        /// <param name="id">房屋Id集合</param>
        /// <returns></returns>
        public static BaseAnswer RepealJinSan(int Jsid, int AcceptanceId)
        {

            string now = DateTime.Now.ToString("yyyy-MM-01");  //取到当前的年月
            int Month = TimeHelper.ToTimeStamp(DateTime.Parse(now)); //把当前年月转换成时间戳
           
            List<string> lstsql = new List<string>();
            //根据Js_Id 获取对应的数据
            string sqlSsearch = "select * from [V_Zor_InputJinSan] where Js_Id=" + Jsid + ""; 
            dynamic repealJinSan = DapperQuery<T>.DoQueryT(sqlSsearch,null);
            //1-a 社区月份统计（有当前月，修改月份统计，没有当前月新增月份统计）
            string SQIdStr = "select Dt_Id from Zor_PayTaxeCount where [Type]=1 and Pt_Date=" + Month + " and Dt_Id =" + repealJinSan.Community_Id + "";
            List<dynamic> IsDt_Id = DapperQuery<dynamic>.DoQueryList(SQIdStr, null); //对比社区ID
            if (IsDt_Id.Count == 0)
            {   // 新增社区月统计 
                lstsql.Add(string.Format("insert into [Zor_PayTaxeCount](ANumber,PNumber,Zzs,Fcs,Grsds,Cjs,Jyffj,Dfjyfj,Yhs,Sljj,Bd_Id,Dt_Id,Stree_ID,Type,Pt_Date) values (1,1," + repealJinSan.Js_Increment + "," + repealJinSan.Js_HouseTax + "," + repealJinSan.Js_PersonTax + "," + repealJinSan.Js_UrbanTax + "," + repealJinSan.Js_Education + "," + repealJinSan.Js_LocalEducation + "," + repealJinSan.Js_Stamp + "," + repealJinSan.Js_WaterTax + "," + repealJinSan.Bd_Id + "," + repealJinSan.Community_Id + "," + repealJinSan.StreetOffice_Id + ",1," + Month + ")"));
            }
            else
            {   //修改社区月统计
                lstsql.Add(string.Format("update [Zor_PayTaxeCount] set ANumber= ANumber+ 1,PNumber=PNumber+1,Zzs=Zzs+" + repealJinSan.Js_Increment + ",Fcs=Fcs+" + repealJinSan.Js_HouseTax + ",Grsds=Grsds+" + repealJinSan.Js_PersonTax + ",Cjs=Cjs+" + repealJinSan.Js_UrbanTax + ",Jyffj=Jyffj+" + repealJinSan.Js_Education + ",Dfjyfj=Dfjyfj+" + repealJinSan.Js_LocalEducation + ",Yhs=Yhs+" + repealJinSan.Js_Stamp + ",Sljj=Sljj+" + repealJinSan.Js_WaterTax + " where type = 1 and Pt_Date = " + Month + " and Dt_Id = " + repealJinSan.Community_Id + " "));
            }

            //1-b 修改社区总统计（在受理单产生时，对应的总统计表已存在）
            lstsql.Add(string.Format("update [Zor_PayTaxeCount] set PNumber=PNumber+1,Zzs=Zzs+" + repealJinSan.Js_Increment + ",Fcs=Fcs+" + repealJinSan.Js_HouseTax + ",Grsds=Grsds+" + repealJinSan.Js_PersonTax + ",Cjs=Cjs+" + repealJinSan.Js_UrbanTax + ",Jyffj=Jyffj+" + repealJinSan.Js_Education + ",Dfjyfj=Dfjyfj+" + repealJinSan.Js_LocalEducation + ",Yhs=Yhs+" + repealJinSan.Js_Stamp + ",Sljj=Sljj+" + repealJinSan.Js_WaterTax + " where type = 1 and Pt_Date =0 and Dt_Id = " + repealJinSan.Community_Id + " "));


            //2-a街道办月份统计
            string JDBIdStr = "select Stree_ID from Zor_PayTaxeCount where [Type]=2 and Pt_Date=" + Month + " and Stree_ID =" + repealJinSan.StreetOffice_Id + " ";
            List<dynamic> IsStree_ID = DapperQuery<dynamic>.DoQueryList(JDBIdStr, null); //对比街道办ID
            if (IsStree_ID.Count == 0)
            {//新增街道办月份统计
                lstsql.Add(string.Format("insert into [Zor_PayTaxeCount](ANumber,PNumber,Zzs,Fcs,Grsds,Cjs,Jyffj,Dfjyfj,Yhs,Sljj,Bd_Id,Dt_Id,Stree_ID,Type,Pt_Date) values (1,1," + repealJinSan.Js_Increment + "," + repealJinSan.Js_HouseTax + "," + repealJinSan.Js_PersonTax + "," + repealJinSan.Js_UrbanTax + "," + repealJinSan.Js_Education + "," + repealJinSan.Js_LocalEducation + "," + repealJinSan.Js_Stamp + "," + repealJinSan.Js_WaterTax + "," + repealJinSan.Bd_Id + "," + repealJinSan.Community_Id + "," + repealJinSan.StreetOffice_Id + ",2," + Month + ")"));
            }
            else
            { //修改街道办月份统计
                lstsql.Add(string.Format("update [Zor_PayTaxeCount] set ANumber= ANumber+ 1,PNumber=PNumber+1,Zzs=Zzs+" + repealJinSan.Js_Increment + ",Fcs=Fcs+" + repealJinSan.Js_HouseTax + ",Grsds=Grsds+" + repealJinSan.Js_PersonTax + ",Cjs=Cjs+" + repealJinSan.Js_UrbanTax + ",Jyffj=Jyffj+" + repealJinSan.Js_Education + ",Dfjyfj=Dfjyfj+" + repealJinSan.Js_LocalEducation + ",Yhs=Yhs+" + repealJinSan.Js_Stamp + ",Sljj=Sljj+" + repealJinSan.Js_WaterTax + " where type = 2 and Pt_Date = " + Month + " and Stree_ID = " + repealJinSan.StreetOffice_Id + " "));
            }
            //2-b街道办总统计           
            lstsql.Add(string.Format("update [Zor_PayTaxeCount] set PNumber=PNumber+1,Zzs=Zzs+" + repealJinSan.Js_Increment + ",Fcs=Fcs+" + repealJinSan.Js_HouseTax + ",Grsds=Grsds+" + repealJinSan.Js_PersonTax + ",Cjs=Cjs+" + repealJinSan.Js_UrbanTax + ",Jyffj=Jyffj+" + repealJinSan.Js_Education + ",Dfjyfj=Dfjyfj+" + repealJinSan.Js_LocalEducation + ",Yhs=Yhs+" + repealJinSan.Js_Stamp + ",Sljj=Sljj+" + repealJinSan.Js_WaterTax + " where type = 2 and Pt_Date = 0 and Stree_ID = " + repealJinSan.StreetOffice_Id + " "));


            //3-a楼宇月统计
            string BdIdStr = "select Bd_Id from Zor_PayTaxeCount where [Type]=3 and Pt_Date=" + Month + " and Bd_Id =" + repealJinSan.Bd_Id + " ";
            List<dynamic> IsBd_ID = DapperQuery<dynamic>.DoQueryList(BdIdStr, null); //对比楼宇ID
            if (IsBd_ID.Count == 0)
            {//新增楼宇月份统计
                lstsql.Add(string.Format("insert into [Zor_PayTaxeCount](ANumber,PNumber,Zzs,Fcs,Grsds,Cjs,Jyffj,Dfjyfj,Yhs,Sljj,Bd_Id,Dt_Id,Stree_ID,Type,Pt_Date) values (1,1," + repealJinSan.Js_Increment + "," + repealJinSan.Js_HouseTax + "," + repealJinSan.Js_PersonTax + "," + repealJinSan.Js_UrbanTax + "," + repealJinSan.Js_Education + "," + repealJinSan.Js_LocalEducation + "," + repealJinSan.Js_Stamp + "," + repealJinSan.Js_WaterTax + "," + repealJinSan.Bd_Id + "," + repealJinSan.Community_Id + "," + repealJinSan.StreetOffice_Id + ",3," + Month + ")"));
            }
            else
            {//修改楼宇月份统计
                lstsql.Add(string.Format("update [Zor_PayTaxeCount] set ANumber= ANumber+ 1,PNumber=PNumber+1,Zzs=Zzs+" + repealJinSan.Js_Increment + ",Fcs=Fcs+" + repealJinSan.Js_HouseTax + ",Grsds=Grsds+" + repealJinSan.Js_PersonTax + ",Cjs=Cjs+" + repealJinSan.Js_UrbanTax + ",Jyffj=Jyffj+" + repealJinSan.Js_Education + ",Dfjyfj=Dfjyfj+" + repealJinSan.Js_LocalEducation + ",Yhs=Yhs+" + repealJinSan.Js_Stamp + ",Sljj=Sljj+" + repealJinSan.Js_WaterTax + " where type = 3 and Pt_Date = " + Month + " and Bd_Id = " + repealJinSan.Bd_Id + " "));           
            }
            //3-b楼宇总统计
            lstsql.Add(string.Format("update [Zor_PayTaxeCount] set PNumber=PNumber+1,Zzs=Zzs+" + repealJinSan.Js_Increment + ",Fcs=Fcs+" + repealJinSan.Js_HouseTax + ",Grsds=Grsds+" + repealJinSan.Js_PersonTax + ",Cjs=Cjs+" + repealJinSan.Js_UrbanTax + ",Jyffj=Jyffj+" + repealJinSan.Js_Education + ",Dfjyfj=Dfjyfj+" + repealJinSan.Js_LocalEducation + ",Yhs=Yhs+" + repealJinSan.Js_Stamp + ",Sljj=Sljj+" + repealJinSan.Js_WaterTax + " where type = 3 and Pt_Date = 0 and Bd_Id = " + repealJinSan.Bd_Id + " "));


            //修改金三状态
            lstsql.Add(string.Format("update [Zor_Jinsan] set Js_State=3 where Js_Id=" + Jsid + " "));
            //修改受理单的受理状态
            lstsql.Add(string.Format("update [Zor_Acceptance] set Acceptance_State=8 where Acceptance_Id=" + AcceptanceId + " "));
            BaseAnswer bar = DapperTransaction.DoTransaction(lstsql); 
            return bar;
        }

    }
}
